﻿// -----------------------------------------------------------------------
// <copyright file="ProfileApproveData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db ProfileApprove
    /// </summary>
    public class ProfileApproveData
    {
        /// <summary>
        /// Get ProfileApprove By Id
        /// </summary>
        /// <param name="profileApproveId">Param profileApproveId</param>
        /// <returns>Return ProfileApprove</returns>
        public ProfileApproveEN GetProfileApproveById(int profileApproveId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                ProfileApproveEN profileApprove = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileApproveGetById, profileApproveId);
                while (reader.Read())
                {
                    profileApprove = new ProfileApproveEN();

                    if (!reader.IsDBNull(0))
                    {
                        profileApprove.ProfileApproveId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileApprove.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        profileApprove.UserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        profileApprove.ProfileStatusId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        profileApprove.ApproveDate = reader.GetDateTime(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        profileApprove.Reason = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        profileApprove.DepartmentId = reader.GetInt32(6);
                    }

                    if (!reader.IsDBNull(7))
                    {
                        profileApprove.ProfileStatusDisplay = reader.GetString(7);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return profileApprove;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all ProfileApprove
        /// </summary>
        /// <returns>Return list all ProfileApprove</returns>
        public IList<ProfileApproveEN> GetProfileApproves()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ProfileApproveEN> lstProfileApprove = new List<ProfileApproveEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileApproveGetAll);
                while (reader.Read())
                {
                    ProfileApproveEN profileApprove = new ProfileApproveEN();

                    if (!reader.IsDBNull(0))
                    {
                        profileApprove.ProfileApproveId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileApprove.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        profileApprove.UserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        profileApprove.ProfileStatusId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        profileApprove.ApproveDate = reader.GetDateTime(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        profileApprove.Reason = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        profileApprove.DepartmentId = reader.GetInt32(6);
                    }

                    if (!reader.IsDBNull(7))
                    {
                        profileApprove.ProfileStatusDisplay = reader.GetString(7);
                    }

                    lstProfileApprove.Add(profileApprove);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstProfileApprove;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db ProfileApprove
        /// </summary>
        /// <param name="profileApprove">Paarm ProfileApprove</param>
        /// <returns>Insert ProfileApprove</returns>
        public int Insert(ProfileApproveEN profileApprove)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ProfileApprove.ProfileId, profileApprove.ProfileId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileApprove.ProfileStatusId, profileApprove.ProfileStatusId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileApprove.UserId, profileApprove.UserId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileApprove.ApproveDate, (object)profileApprove.ApproveDate ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileApprove.Reason, profileApprove.Reason),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileApprove.DepartmentId, profileApprove.DepartmentId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileApprove.ProfileStatus, profileApprove.ProfileStatusDisplay)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileApproveInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db ProfileApprove
        /// </summary>
        /// <param name="profileApprove">Param ProfileApprove</param>
        public void Update(ProfileApproveEN profileApprove)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ProfileApprove.ProfileApproveId, profileApprove.ProfileApproveId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileApprove.ProfileId, profileApprove.ProfileId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileApprove.ProfileStatusId, profileApprove.ProfileStatusId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileApprove.UserId, profileApprove.UserId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileApprove.ApproveDate, (object)profileApprove.ApproveDate ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileApprove.Reason, profileApprove.Reason),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileApprove.DepartmentId, profileApprove.DepartmentId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileApprove.ProfileStatus, profileApprove.ProfileStatusDisplay)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileApproveUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete ProfileApprove by profileApproveId
        /// </summary>
        /// <param name="profileApproveId">param profileApproveId</param>
        public void Delete(int profileApproveId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.ProfileApprove.ProfileApproveId, profileApproveId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileApproveDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get List ProfileApprove By profileId
        /// </summary>
        /// <param name="profileId">Param profileId</param>
        /// <returns>Return List ProfileApprove</returns>
        public IList<ProfileApproveEN> GetProfileApproveByProfileId(int profileId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ProfileApproveEN> lstProfileApprove = new List<ProfileApproveEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileApproveGetByProfileId, profileId);
                while (reader.Read())
                {
                    ProfileApproveEN profileApprove = new ProfileApproveEN();

                    if (!reader.IsDBNull(0))
                    {
                        profileApprove.ProfileApproveId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileApprove.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        profileApprove.UserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        profileApprove.ProfileStatusId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        profileApprove.ApproveDate = reader.GetDateTime(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        profileApprove.Reason = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        profileApprove.DepartmentId = reader.GetInt32(6);
                    }

                    if (!reader.IsDBNull(7))
                    {
                        profileApprove.ProfileStatusDisplay = reader.GetString(7);
                    }

                    lstProfileApprove.Add(profileApprove);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstProfileApprove;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}